<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<style>

.cov{
	position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, .2);
    display: none;
}
.con{
	width:600px;
	height:200px;
    position: absolute;
	background-color:white;
	left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}

p  {
    text-align: center;
    line-height: 200px;
}
</style>

<body>

    <div class = "cov">
        <div class = "con">
            <p>弹层内容</p>
        </div>
    </div>
    <button>点我出现弹层</button>
    
</body>

<script>
    let bt=document.querySelector('button')
    let cov=document.querySelector('.cov')
    let con=document.querySelector('.con')
    

    //点击按钮出现弹层 （阻止冒泡）
    bt.onclick=function(e) {
        cov.style.display='block'
    }

    // 在弹层里点击时候不关闭弹层（阻止冒泡）
    con.onclick=function(e) {
        e.stopPropagation()
    }

    // 点击页面关闭弹层
    cov.onclick=function() {
       cov.style.display='none'
    }

   
</script>

</html>